//
//  BillsPickerView.m
//  Thinkape-iOS
//
//  Created by admin on 16/6/1.
//  Copyright © 2016年 TIXA. All rights reserved.
//

#import "BillsPickerView.h"
#import "KindsModel.h"
#import "BFKit.h"
#import "AppDelegate.h"


@interface BillsPickerView ()

@property (strong, nonatomic) UIPickerView *pickerView;
@property (strong, nonatomic) KindsModel *selectModel;
@property (strong ,nonatomic) KindsMainModel *mainModel;
@end

@implementation BillsPickerView

{
    //    NSArray *titleArr;
    NSInteger _index;
    NSInteger _colum_Two;

}


- (void)awakeFromNib
{
    self.selectModel = [[KindsModel alloc] init];
    if (!self.pickerView) {
        self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 36, SCREEN_WIDTH, 162)];
        self.pickerView.delegate = self;
        self.pickerView.dataSource = self;
        [self addSubview:self.pickerView];
    }
    
}

- (void)setDataArray:(NSArray *)dataArray{
    if (self.dataArray != dataArray) {
        _dataArray = dataArray;
        _selectModel = [[_dataArray firstObject] firstObject];
        [self.pickerView reloadAllComponents];
        for (int i = 0; i < 2; i ++) {
            NSInteger row = [self.pickerView selectedRowInComponent:i];
            [self resetColor:row component:i];
        }
        
    }
}


- (void)setTitleArr:(NSArray *)titleArr
{
    if (self.titleArr != titleArr) {
        _titleArr = titleArr;
    }
}

#pragma mark - PickerViewDelegate

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return _titleArr.count;
    }
    else
    {
        NSArray *arr = [self.dataArray safeObjectAtIndex:_index];
        if (arr) {
            return arr.count;
        }
        else
            return 0;
    }
    
        

}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)vie{
    
    UILabel *label = [[UILabel alloc] init];
    if (component == 0) {
        [label setFrame:CGRectMake(0, 0, 100, 15)];
        KindsMainModel *model = [_titleArr safeObjectAtIndex:row];
        label.text = model.menuname;
        
        //wo
        AppDelegate *app=[UIApplication sharedApplication ].delegate;
        NSLog(@"传值%@",app.zhuanStr);
        
        app.zhuanStr=label.text;
    }
    else{
        
        [label setFrame:CGRectMake(0, 0, 160, 15)];
        KindsModel *model;

        NSArray *arr = [self.dataArray safeObjectAtIndex:_index];
        model = [arr safeObjectAtIndex:row];
        label.text = model.cname;
        label.textAlignment = 1;
    }
    
    label.font = [UIFont systemFontOfSize:15];
    label.textColor = [UIColor colorWithHexString:@"333333"];
    return label;
    
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    if (component == 0) {
        return 100;
    }
    else
        return 160;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 35;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    if (component == 0) {
        _index = row;
        _colum_Two = 0;
        [self.pickerView reloadComponent:1];
        [self resetColor:_colum_Two component:1];
    }
    else if (component ==1){
        _colum_Two = row;
        [self resetColor:row component:component];
    }
}

- (void)resetColor:(NSInteger )row component:(NSInteger )component{
    NSArray *arr = [self.dataArray safeObjectAtIndex:_index];
   
        // NSArray *arr = [self.dataArray safeObjectAtIndex:row];
    _selectModel = [arr safeObjectAtIndex:row];

    _mainModel = [self.titleArr safeObjectAtIndex:_index];
    
    NSLog(@"_selectModel :%@",_selectModel);
    UILabel *label = (UILabel *)[self.pickerView viewForRow:row forComponent:component];
    label.textColor = [UIColor colorWithRed:0.231 green:0.482 blue:0.824 alpha:1.000];
}

#pragma mark - 点击确定\取消按钮 调用

- (IBAction)SureBtnClick:(id)sender {
    self.transform = CGAffineTransformMakeTranslation(0, 799);
    if(self.selectItemCallBack){
        self.selectItemCallBack(_mainModel,_selectModel);
    }
    [self removeFromSuperview];
}

- (IBAction)CancelBtnClick:(id)sender {
 
    if (self.cancelCallback) {
        self.cancelCallback();
    }
    
    [self removeFromSuperview];
}


@end
